<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use DB;
use Illuminate\Support\Facades\Redis;
use App\Model\AdminOrder;

/**
 * 这个类用来显示用户订单的数据以及对订单进行发货等数据处理
 * @author guoguo<[gaymbiu@163.com]>
 */
class AdminOrderController extends Controller
{
    /**
     * Display a listing of the resource.
     *显示订单主页
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        $orders = DB::table('shop_orders')
                ->select('id', 'user_id', 'o_num', 'getname', 'address', 'phone',  'score', 'discount', 'total', 'create_at', 'send_at', 'status')
                ->orderBy('id')
                ->paginate(10);
        return view('Admin.order.orderlist', ['orders' => $orders]);
    }

    /**
     * Show the form for creating a new resource.
     *处理订单搜索功能
     * @return \Illuminate\Http\Response
     */
    public function orderSearch(Request $request)
    {
        //接收并拼接get传过来的数据
        $start_date = $request->start_date;
        $end_date = $request->end_date;
        //编号
        $o_num = intval($request->o_num);
        //状态
        $status = intval($request->status);
        $user_id = intval($request->user_id);
        //判断搜索条件的组合,如果搜索条件为空就直接跳转到订单列表页
        if (!$start_date  && !$end_date && !$o_num && !$status && !$user_id) {
            return redirect()->route('order');
        }

        // 走到这里说明搜索条件有数据了，再对条件组合拼接
        $where = [];
        if ($user_id) {
            $where[] = ['user_id', $user_id];
        }

        if ($status) {
            $where[] = ['status', $status];
        }

        if ($o_num) {
            $where[] = ['o_num', 'like', '%'.$o_num.'%'];
        }

        if (empty($start_date) && $end_date) {
            $where[] = ['create_at', '<', $end_date];
        }

        if ($start_date && empty($end_date)) {
            $where[] = ['create_at', '>', $start_date];
        }

        if ($start_date && $end_date) {
            $where[] = ['create_at', '>', $start_date];
            $where[] = ['create_at', '<', $end_date];
        }

        $orders = DB::table('shop_orders')
                 ->select('id', 'user_id', 'o_num', 'getname', 'address', 'phone',  'score', 'discount', 'total', 'create_at', 'send_at', 'status')
                 ->where($where)
                 ->orderBy('id')
                 ->paginate(10);

        return view('Admin.order.orderlist', ['orders' => $orders, 'start_date' =>$start_date, 'end_date' => $end_date, 'o_num' => $o_num, 'status' => $status, 'user_id' => $user_id]);

    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        //
    }

    /**
     * Display the specified resource.
     *利用数据关联,展示订单详情页
     * @param  int  $id 订单id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        $details = AdminOrder::find($id)->hasManyOrderdetail;
        // dd($res);
        return view('Admin/order/order-detail', ['details' => $details]);
    }


    /**
     * Show the form for editing the specified resource.
     * 这个方法用来处理发货，改变订单状态并追加发货时间
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function orderGo(Request $request)
    {
        $id = $request->id;
        $date = date('Y-m-d H:i:s');

        //通过前台AJAX传递过来的id去修改订单的状态并追加发货时间
        $res = DB::table('shop_orders')
                 ->where('id', $id)
                 ->update([
                     'status' => 5,
                     'send_at' => date('Y-m-d H:i:s'),
                ]);
        //如果修改成功就返回正常状态回去
        if ($res) {
            return response()->json([
                'status' => 200,
                'msg' => 'OK',
                'date' => $date,
            ]);
        }

        //修改失败就返回错误码回去
        return response()->json([
            'status' => 404,
            'msg' => '修改失败',
        ]);
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        //
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        //
    }


    // public function orderdetails($id)
    // {
    //     $res = AdminOrder::find($id)->hasManyOrderdetail;
    //     dd($res[0]);
    // }

    public function sessionTest(Request $request)
    {

    }

}
